package com.scedu.controller;

import com.scedu.dao.DepartmentDao;
import com.scedu.dao.EmployeeDao;
import com.scedu.entity.Department;
import com.scedu.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @RequestMapping("/employee")
    public String employee(Model model){
        Collection<Employee> employees = employeeDao.getall();

        model.addAttribute("employees",employees);
        return "list";
    }
    //跳转到添加
    @Autowired
    DepartmentDao departmentDao;
    @GetMapping("/toadd")
    public String toadd(Model model){
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departs",departments);
        return "add";
    }
    //添加员工
    @PostMapping("/doadd")
    public String doadd(@RequestParam("lastName") String lastName,
                        @RequestParam("email") String email,
                        @RequestParam("gender") Integer gender,
                        @RequestParam("department") Integer department,
                        @RequestParam("birth") String birth,
                        Model model){
        Employee employee=new Employee();
        employee.setLastName(lastName);
        employee.setEmail(email);
        employee.setGender(gender);
        //处理部门
        Department department1=departmentDao.getDepartmentById(department);
        employee.setDepartment(department1);
        //处理日期
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
        try {
            Date date = sdf.parse( birth );
            employee.setBirth(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //System.out.println(employee);
        employeeDao.add(employee);

        return "redirect:employee";
    }
    //跳转到修改
    @GetMapping("/toedit")
    public String toedit(Integer id,Model model){


        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);

        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departs",departments);
        return "edit";
    }
    //修改员工信息
    @PostMapping("/doedit")
    public String doedit(Employee employee,
                         Model model){

        //System.out.println(employee);
        employeeDao.add(employee);

        return "redirect:/employee";
    }
    //删除员工信息
    @GetMapping("/del/{id}")

    public String del(@PathVariable("id") Integer id,
                         Model model){

        //System.out.println(id);
        employeeDao.delEmployeeById(id);

        return "redirect:/employee";
    }
}
